package com.turing.draw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.turing.common.util.FileUtils;
import com.turing.draw.model.domain.FileImage;
import com.turing.draw.model.domain.FileTask;
import com.turing.draw.service.FileTaskService;
import com.turing.draw.service.PDFService;
import com.turing.draw.service.FileImageService;
import lombok.Data;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class PDFServiceImpl implements PDFService {

    private static Log logger = LogFactory.getLog( PDFServiceImpl.class );

    @Value( "${fdfs.domain}" )
    private String FDFS_DOMAIN;

    @Autowired
    private FileImageService fileImageService;

    @Autowired
    private FileTaskService fileTaskService;

    @Override
    public String requirePageImgPath(String childTaskSn, Integer page) {

        FileImage fileImage = fileImageService.lambdaQuery().eq(FileImage::getTaskSn, childTaskSn)
                .eq(FileImage::getPageNo, page)
                .eq(FileImage::getIsDel,0)
                .one();

        if(null != fileImage ){

            logger.info( ">>>> task sn is " + childTaskSn );
            return FDFS_DOMAIN + fileImage.getImageUrl();

        }

        logger.error( ">>>>>>> file page is null" );
        return "";

    }

    public Map< String, List<Map< String, String>>> scoreOCRQuality(String parentTaskSn ) {
        Map<String, List<Map<String, String>>> results = new LinkedHashMap<>(  );
        List<FileTask> childTasks = fileTaskService.getChildTasks( parentTaskSn );
        if ( childTasks!=null && childTasks.size()>0 ) {
            for ( FileTask childTask : childTasks ) {
                String fileAnalyzePath = childTask.getJSONUrl();
                String taskName = childTask.getFileName();

                String s = FileUtils.loadJson( fileAnalyzePath );
                JSONArray ocrPageArr = JSONArray.parseArray( s );
                if ( ocrPageArr!=null && ocrPageArr.size()>0 ) {
                    LinkedList<Map<String, String>> pagesList = new LinkedList<>(  );
                    for ( int i = 0; i < ocrPageArr.size(); i++ ) {
                        LinkedHashMap<String, String> pageMaps = new LinkedHashMap<>(  );

                        JSONObject pageObj = ocrPageArr.getJSONObject( i );
                        JSONObject imageQualityMeasure = pageObj.getJSONObject( "imageQualityMeasure" );
                        if ( imageQualityMeasure == null ) {
                            imageQualityMeasure = new JSONObject(  );
                        }
                        String tiltAngle = imageQualityMeasure.getString( "tiltAngle" );
                        String imageFuzzy = imageQualityMeasure.getString( "imageFuzzy" );
                        String textRecConfidence = imageQualityMeasure.getString( "textRecConfidence" );

                        String pageNo = pageObj.getString( "pageNo" );

                        pageMaps.put( "pageNo", pageNo==null?"":(Integer.parseInt( pageNo ) + 1)+"" );
                        pageMaps.put( "tiltAngle", tiltAngle==null?"":tiltAngle );
                        pageMaps.put( "imageFuzzy", imageFuzzy==null?"":imageFuzzy );
                        pageMaps.put( "textRecConfidence", textRecConfidence==null?"":textRecConfidence );

                        pagesList.add( pageMaps );

                    }
                    results.put( taskName, pagesList );
                }


            }
        }
        return results;
    }

    @Data
    public static class FilePage {

        private String pagePath;

        private Integer width;

        private Integer height;

        private Integer page;
    }

}
